#region Imported Namespaces
using System;
using System.Collections;

using Utility = PHSRAG.Utility;
#endregion

namespace PHSRAG.Insight21.InsightUtilities
{
	#region Email Class
	/// <summary>
	/// Provides helper functions for Send By Email functionality and any future e-mail functionality.
	/// </summary>
	/// <remarks>
	/// <p>The public "xxKey" accessors provided by this class act as keys into the summaryInfo Hashtable used
	/// in the InsightDownloadBar to allow reporting of basic financial summary info in the e-mail, with only
	/// the information the page actually has being filled out.</p>
	/// <p>With the exception of FundNumberKey, the summary keys provided here correspond to total information,
	/// so if the page does not have a particular total available to it, no key in the summaryInfo Hashtable needs
	/// to be added for it, it will simply be ignored and not included in the e-mail.</p>
	/// <p>The FundNumberKey is used for the subject line of the e-mail. If the detail page sending the e-mail
	/// represents some detail of one particular fund, set the value for summaryInfo[FundNumberKey] to this number
	/// and the e-mail subject will represent that it is for that single fund. Otherwise, don't set it to anything
	/// and the subject will be more generic.</p>
	/// </remarks>
	public class Email
	{
		#region Constants
		private const string FundNumberHashKey = "FundNumber_CBB4A5AE-4A0F-445a-8618-C8CADD08ABEF";
		private const string CashBalanceHashKey = "CashBalance_CBB4A5AE-4A0F-445a-8618-C8CADD08ABEF";
		private const string AwardedBudgetHashKey = "AwardedBudget_CBB4A5AE-4A0F-445a-8618-C8CADD08ABEF";
		private const string DepositedIncomeHashKey = "DepositedIncome_CBB4A5AE-4A0F-445a-8618-C8CADD08ABEF";
		private const string ExpenditureTotalHashKey = "ExpenditureTotal_CBB4A5AE-4A0F-445a-8618-C8CADD08ABEF";
		private const string UnexpensedObligationsHashKey = "UnexpensedObligations_CBB4A5AE-4A0F-445a-8618-C8CADD08ABEF";
		private const string OriginalOrderAmountHashKey = "OriginalOrderAmount_CBB4A5AE-4A0F-445a-8618-C8CADD08ABEF";
		private const string OriginalOrderAmountPaidToDateHashKey = "OriginalOrderAmountPaidToDate_CBB4A5AE-4A0F-445a-8618-C8CADD08ABEF";
		private const string BalanceRemainingHashKey = "BalanceRemaining_CBB4A5AE-4A0F-445a-8618-C8CADD08ABEF";
		private const string CurrentInvoiceAmountHashKey = "CurrentInvoiceAmount_CBB4A5AE-4A0F-445a-8618-C8CADD08ABEF";
		#endregion

		#region Properties
		/// <summary>
		/// Get Fund Number hash key
		/// </summary>
		public static string FundNumberKey
		{
			get { return FundNumberHashKey; }
		}

		/// <summary>
		/// Get Cash Balance hash key
		/// </summary>
		public static string CashBalanceKey
		{
			get { return CashBalanceHashKey; }
		}

		/// <summary>
		/// Get Awarded Budget hash key
		/// </summary>
		public static string AwardedBudgetKey
		{
			get { return AwardedBudgetHashKey; }
		}

		/// <summary>
		/// Get Deposited Income hash key
		/// </summary>
		public static string DepositedIncomeKey
		{
			get { return DepositedIncomeHashKey; }
		}

		/// <summary>
		/// Get Expenditure Total hash key
		/// </summary>
		public static string ExpenditureTotalKey
		{
			get { return ExpenditureTotalHashKey; }
		}

		/// <summary>
		/// Get Unexpensed Obligations hash key
		/// </summary>
		public static string UnexpensedObligationsKey
		{
			get { return UnexpensedObligationsHashKey; }
		}

		/// <summary>
		/// Get Original Order Amount hash key
		/// </summary>
		public static string OriginalOrderAmountKey
		{
			get { return OriginalOrderAmountHashKey; }
		}

		/// <summary>
		/// Get Original Order Amount Paid to Date hash key
		/// </summary>
		public static string OriginalOrderAmountPaidToDateKey
		{
			get { return OriginalOrderAmountPaidToDateHashKey; }
		}

		/// <summary>
		/// Get Balance Remaining hash key
		/// </summary>
		public static string BalanceRemainingKey
		{
			get { return BalanceRemainingHashKey; }
		}

		/// <summary>
		/// Get Current Invoice Amount hash key
		/// </summary>
		public static string CurrentInvoiceAmountKey
		{
			get { return CurrentInvoiceAmountHashKey; }
		}
		#endregion

		#region Public Methods
		/// <summary>
		/// <p>Constructs a properly formed e-mail body for Send By Email functionality. Use this method to avoid
		/// potential naming conflicts when forming a MailMessage object for Send By Email event handlers.</p>
		/// <p>The string returned by this method will have the following form, which can be assigned to the MailMessage's Body field:</p>
		/// 
		/// <p>"The current cash balance is [x]
		/// <br/>The current awarded budget is [x]
		/// <br/>The current deposited income total is [x]
		/// <br/>The current expenditure total is [x]
		/// <br/>The current unexpensed obligations are [x]
		/// <br/>The original order amount is [x]
		/// <br/>The original order amount paid to date is [x]
		/// <br/>The balance remaining is [x]
		/// <br/>The current invoice amount is [x]</p>
		/// 
		/// <p>To view the statement details, please click on the following link:
		/// <br/>[URL to the Insight 2.1 Login page with properly formed query string]</p>
		/// 
		/// <p>[Optional user-defined message]"</p>
		/// 
		/// <p>Note that the exact format of the pre-defined strings in this message is controlled by Web.Config and is subject to change.
		/// This example is merely to provide a guide to the expected template.</p>
		/// <p>If one of the given total variables is not present, that line will not display.</p>
		/// <p>In order for the summary info to be correctly set, the caller must create a Hashtable using the public keys provided
		/// by this class.</p>
		/// </summary>
		/// <param name="webConfigPageURLKey">The calling page's URL key as listed in Web.config</param>
		/// <param name="queryId">The unique ID returned by the InsertPageQuery stored procedure</param>
		/// <param name="optionalUserMessage">Optional, user-defined message to be added to the e-mail</param>
		/// <returns>Properly formed e-mail body for Send By Email functionality</returns>
		public static string BuildEmailBody(string webConfigPageURLKey, int queryId, Hashtable summaryInfo, string optionalUserMessage)
		{
			try
			{
				if (webConfigPageURLKey != null && queryId >= 0)
				{
					if (summaryInfo == null) summaryInfo = new Hashtable();

					string cashBalance = summaryInfo[CashBalanceKey] as string;
					string awardedBudget = summaryInfo[AwardedBudgetKey] as string;
					string depositedIncome = summaryInfo[DepositedIncomeKey] as string;
					string expenditureTotal = summaryInfo[ExpenditureTotalKey] as string;
					string unexpensedObligations = summaryInfo[UnexpensedObligationsKey] as string;
					string originalOrderAmount = summaryInfo[OriginalOrderAmountKey] as string;
					string originalOrderAmountPaidToDate = summaryInfo[OriginalOrderAmountPaidToDateKey] as string;
					string balanceRemaining = summaryInfo[BalanceRemainingKey] as string;
					string currentInvoiceAmount = summaryInfo[CurrentInvoiceAmountKey] as string;

					if (cashBalance != null)
					{
						cashBalance = String.Format("{0}{1}<br />",
													Utility.Settings.GetKeyValue("Email.CashBalance", ""),
													cashBalance);
					}
					else cashBalance = "";

					if (awardedBudget != null)
					{
						awardedBudget = String.Format("{0}{1}<br />",
													Utility.Settings.GetKeyValue("Email.AwardedBudget", ""),
													awardedBudget);
					}
					else awardedBudget = "";

					if (depositedIncome != null)
					{
						depositedIncome = String.Format("{0}{1}<br />",
													Utility.Settings.GetKeyValue("Email.DepositedIncome", ""),
													depositedIncome);
					}
					else depositedIncome = "";

					if (expenditureTotal != null)
					{
						expenditureTotal = String.Format("{0}{1}<br />",
													Utility.Settings.GetKeyValue("Email.ExpenditureTotal", ""),
													expenditureTotal);
					}
					else expenditureTotal = "";

					if (unexpensedObligations != null)
					{
						unexpensedObligations = String.Format("{0}{1}<br />",
													Utility.Settings.GetKeyValue("Email.UnexpensedObligations", ""),
													unexpensedObligations);
					}
					else unexpensedObligations = "";

					if (originalOrderAmount != null)
					{
						originalOrderAmount = String.Format("{0}{1}<br />",
													Utility.Settings.GetKeyValue("Email.OriginalOrderAmount", ""),
													originalOrderAmount);
					}
					else originalOrderAmount = "";

					if (originalOrderAmountPaidToDate != null)
					{
						originalOrderAmountPaidToDate = String.Format("{0}{1}<br />",
													Utility.Settings.GetKeyValue("Email.OriginalOrderAmountPaidToDate", ""),
													originalOrderAmountPaidToDate);
					}
					else originalOrderAmountPaidToDate = "";

					if (balanceRemaining != null)
					{
						balanceRemaining = String.Format("{0}{1}<br />",
													Utility.Settings.GetKeyValue("Email.BalanceRemaining", ""),
													balanceRemaining);
					}
					else balanceRemaining = "";

					if (currentInvoiceAmount != null)
					{
						currentInvoiceAmount = String.Format("{0}{1}<br />",
													Utility.Settings.GetKeyValue("Email.CurrentInvoiceAmount", ""),
													currentInvoiceAmount);
					}
					else currentInvoiceAmount = "";

					string link = String.Format("<a href=\"{0}{1}&{2}{3}\">{4}</a>",
													Utility.Settings.GetKeyValue("Email.LinkBeginning", ""),
													webConfigPageURLKey,
													Utility.Settings.GetKeyValue("Email.LinkMiddle", ""),
													queryId,
													Utility.Settings.GetKeyValue("Email.LinkText", "Click here"));

					string body = "";

					if (optionalUserMessage == null ) optionalUserMessage = "";

					body = String.Format("{0}{1}{2}{3}{4}{5}{6}{7}{8}<br />{9}<br />{10}<br /><br />{11}",
													cashBalance,
													awardedBudget,
													depositedIncome,
													expenditureTotal,
													unexpensedObligations,
													originalOrderAmount,
													originalOrderAmountPaidToDate,
													balanceRemaining,
													currentInvoiceAmount,
													Utility.Settings.GetKeyValue("Email.LinkHeader", ""),
													link,
													optionalUserMessage);

					return body;
				}
				else
					return null;
			}
			catch(Exception e)
			{
				Utility.ExceptionReport.WriteToEventLog ("BuildEmailBody formatting", e, true, Utility.ExceptionReport.ReportType.Error);
				return null;
			}
		}
		#endregion

		#region Constructor
		private Email()
		{
		}
		#endregion
	}
	#endregion
}
